<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<%
	String path = request.getContextPath();
%>
<title>系统管理</title>
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/btn_class.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/style.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/extjs/ext-patch.css" />

<script type="text/javascript"
	src="<%=path%>/extjs/adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="<%=path%>/extjs/ext-all.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
	<div id="west-div" style="width: 100%; height: 100%;"></div>
	<div id="center-div"></div>
</body>
<script type="text/javascript">
	Ext.BLANK_IMAGE_URL = "<%=path%>/extjs/resources/images/default/s.gif";

	Ext.onReady(function() {
				Ext.QuickTips.init();
				
				// shorthand
				var Tree_op = Ext.tree;
				var loader_op = new Tree_op.TreeLoader({
					dataUrl : 'office!buildOfficeTree.do'
				});
				loader_op.on("beforeload", function(treeLoader, node) {
					//loader_op.baseParams.fcode = Ext.get("r_id").dom.value;//角色id
					//loader.baseParams.fcode  = node.attributes.fcode;
				}, this);

				loader_op.processResponse = function(response, node, callback) {
					var json1 = response.responseText;
					try {
						var json = eval("(" + json1 + ")");
						node.beginUpdate();
						//从json中取得json数组
						var o = json["key"];
						for ( var i = 0, len = o.length; i < len; i++) {
							var n = this.createNode(o[i]);
							if (n) {
								node.appendChild(n);
							}
						}
						node.endUpdate();
						if (typeof callback == "function") {
							callback(this, node);
						}
					} catch (e) {
						this.handleFailure(response);
					}
				};

				var tree_op = new Tree_op.TreePanel({
					region : 'center',
					id : 'west-panel',
					el : 'west-div',
					width : 280,
					//	title : '所属区县',
					useArrows : true,
					autoScroll : true,
					animate : true,
					enableDD : true,
					containerScroll : true,
					rootVisible : false, //树根不可见
					loader : loader_op
				});
				
				//单击事件
				tree_op.on('click', function(node) {
					ds.removeAll();
						ds.load( {
							params : {
								start : 0,
								limit : 50,
								foffice : node.id,
								fstate : '1',
								fkeywords : Ext.get('fkeywords_id').dom.value,
								fbtime : Ext.get('fbtime_id').dom.value,
								fetime : Ext.get('fetime_id').dom.value
								
							}
						});
				}, tree_op);
				
				/*
				//checkBox勾选事件
				tree_op.on('checkchange', function(node, checked) {
					node.expand();
					node.attributes.checked = checked;
					node.eachChild(function(child) {
						child.ui.toggleCheck(checked);
						child.attributes.checked = checked;
						child.fireEvent('checkchange', child, checked);
					});
				}, tree_op);
                */
                
				// set the root node
				var root_op = new Tree_op.AsyncTreeNode({
					text : '所有栏目',
					draggable : false,
					id : '1',
					fid : '1',
					ftype : '1'
				});
				tree_op.setRootNode(root_op);

				
				
				var Jthtwb = Ext.data.Record.create([ {
					name : 'fid',
					mapping : 'fid',
					type : 'string'
				}, {
					name : 'fname',
					mapping : 'fname',
					type : 'string'
				}, {
					name : 'fdesc',
					mapping : 'fdesc',
					type : 'string'
				}, {
					name : 'fcdate',
					mapping : 'fcdate',
					type : 'string'
				} ]);

				var ds = new Ext.data.Store({
					reader : new Ext.data.JsonReader({
						totalProperty : 'totalProperty',
						root : 'root'
					}, Jthtwb),
					idProperty : 'threadid',
					remoteSort : true,
					url : 'role!queryRoleList.do'

				});

				ds.load({
					params : {
						start : 0,
						limit : 30
					}
				});


				//定义勾选框，不需要可不必定义
				var sm = new Ext.grid.CheckboxSelectionModel({
					singleSelect : true
				});

				//定义列
				var colM = new Ext.grid.ColumnModel([ sm, new Ext.grid.RowNumberer(),//增加自动编号，不需要可不必定义
				    {
						header : '角色名称',
						dataIndex : 'fname',
						width : 150,
						sortable : false
					}, {
						header : '备注',
						dataIndex : 'fdesc',
						width : 400,
						renderer : function(value) {
								if (value == "null") {
									return "";
								} else {
									return value;
								}
							}
					}, {
						header : '创建日期',
						dataIndex : 'fcdate',
						width : 100,
						renderer : function(value) {
								if (value == "null") {
									return "";
								} else {
									return value;
								}
							}
					} ]);

				
				
				//生成表格
				var grid = new Ext.grid.GridPanel({
					closable : false,
					region : 'center',
					height : 350,
					autoWidth : true,
					title : '角色列表',
					cm : colM,
					ds : ds,
					sm : sm,
					listeners : {
						rowdblclick : function(grid, row) { //双击打开
							var record = grid.getSelectionModel().getSelected();
							if (record) {
								role_detail_form();
								Ext.get("role_name").dom.value = record.data.fname;
								Ext.get("role_desc").dom.value = record.data.fdesc;
								Ext.get("role_id").dom.value = record.data.fid;
							}
						}
					}
				});

				var btn_add = new Ext.Button({
					text : '新增',
					iconCls : 'sy-add',
					handler : function() {
						role_detail_form();
					}
				});

				
				
				/**************************** 以下是新增群组表单 *************************************************/
				//表单
				function role_detail_form() {

					var role_form_send = new Ext.FormPanel(
							{
								region : 'center',
								url : 'role!addRole.do',
								method : 'POST',
								labelWidth : 55,
								frame : true,
								labelAlign : 'right',
								autoScroll : true,
								border : false,

								items : [ {
										columnWidth : .98,
										layout : "form",
										baseCls : "x-plain",
										labelWidth : 60,
										bodyStyle : 'padding-top:5px;',
										frame : true,
										items : [{
											xtype : 'textfield',
											fieldLabel : '角色名称',
											id : 'role_name',
											name : 'fname',
											readOnly : false,
											allowBlank : false,
											width : 260
										}, {
											xtype : 'textarea',
											fieldLabel : '备注',
											id : 'role_desc',
											name : 'fdesc',
											readOnly : false,
											allowBlank : true,
											height : 60,
											width : 260
										}, {
											xtype : 'hidden',
											fieldLabel : '角色id',
											id : 'role_id',
											name : 'fid'
										} ]
									} ]
							});

					var btn_save_form = new Ext.Button({
						text : '保存',
						iconCls : 'sy-save',
						handler : function(btn) {

							var frm = role_form_send.getForm();
							if (frm.isValid()) {

								frm.submit({
								    url : 'role!addRole.do',
								    method : 'POST',
									waitTitle : '请稍候',
									waitMsg : '正在提交表单数据,请稍候...',
									success : function(form, action) {
										ds.reload();
										role_detail_window.close();
									},
									failure : function(form, action) {
										Ext.Msg.show({
											title : '错误提示',
											msg : action.result.message,
											buttons : Ext.Msg.OK,
											fn : function() {
												unfield.focus(true);
												btn.enable();
											},
											icon : Ext.Msg.ERROR
										});
									},// failure
									icon : Ext.Msg.ERROR
								});
							}
							;// isValid
						}
					});

					var btn_reset_form = new Ext.Button({
						text : '取消',
						iconCls : 'sy-reset',
						handler : function(btn) {
							role_detail_window.close();
						}

					});

					var role_detail_window = new Ext.Window({
						tbar : [ btn_save_form, '-', btn_reset_form ],
						width : 380,
						height : 170,
						title : '新增角色',
						iconCls : 'sy-win-flag',
						modal : true,
						items : [ role_form_send ]
					});
					role_detail_window.show();

				} // end form

				var btn_delete = new Ext.Button({
					text : '删除',
					iconCls : 'sy-delete',
					handler : function() {
						var record = grid.getSelectionModel().getSelected();
						if (record) {
							Ext.Msg.confirm('确认删除', '删除操作将会导致被授权该角色的用户无法正常使用系统，您确定删除吗？', function(btn) {
								if (btn == 'yes') {
									Ext.Ajax.request({
										url : 'role!deleteRoleById.do',
										params:{
						    	        	fid : record.data.fid
							    	    },
										success : function() {
											ds.removeAll();
											ds.reload();
											Ext.MessageBox.alert('提示', '删除记录成功！');
										},
										failure : function() {
											Ext.Msg.show({
												title : '错误提示',
												msg : '删除记录失败！',
												buttons : Ext.Msg.OK,
												icon : Ext.Msg.ERROR
											});
										}
									});
								}
							});

						} else {
							Ext.MessageBox.alert('提示', '请选择一行纪录进行操作！');
							return;
						}
					}
				});
				
				var btn_sq = new Ext.Button({
							text : '修改权限',
							iconCls : 'sy-star',
							handler : function() {
								var record = grid.getSelectionModel().getSelected();
								if (record) {
									Ext.get("r_id").dom.value = record.get("fid");
									Ext.get("r_name").dom.value = record.get("fname");
									tree_op.root.reload();
									tree_op.root.expand(true, true);
									//role_column_fun(record.get("fid"));
								} else {
									Ext.MessageBox.alert('提示', '请选择一条记录进行操作！');
									return false;
								}
							}
						});
				
				var btn_refresh = new Ext.Button({
					text : '刷新',
					iconCls : 'sy-refresh',
					handler : function() {
						ds.removeAll();
						ds.reload();
					}
				});
				
				var btn_save_qx = new Ext.Button({
					text : '保存权限',
					iconCls : 'sy-sure',
					handler : function() {
						var checkedNodes = tree_op.getChecked();
						var checked_idStr = "";
						for(var i=0;i<checkedNodes.length;i++) {
						      checked_idStr += checkedNodes[i].id + ",";
						}
						checked_idStr = checked_idStr.substring(0,
								checked_idStr.length - 1);
						Ext.Msg.confirm('确认', '您确认要更改<font color=red>'+Ext.get("r_name").dom.value+'</font>的权限吗？', function(btn) {
							if (btn == 'yes') {
								Ext.Ajax.request({
									url : 'role!saveRoleColumns.do',
									method : 'POST',
									params : {
										"fid" : Ext.get("r_id").dom.value,
										"ids" : checked_idStr
									},
									success : function(response, action) {
										Ext.Msg.alert('', '保存成功！');
									},
									// 提交失败的回调函数
									failure : function() {
										Ext.Msg.alert('错误', '服务器出现错误请稍后再试！');
									}
								});
							}
						});
					}
				});
				
				var btn_clear_qx = new Ext.Button({
					text : '清空权限',
					iconCls : 'sy-cancel',
					handler : function() {
						Ext.Msg.confirm('确认', '您确认要清空<font color=red>'+Ext.get("r_name").dom.value+'</font>的权限吗？', function(btn) {
							if (btn == 'yes') {
								Ext.Ajax.request({
									url : 'role!clearRoleColumns.do',
									method : 'POST',
									params : {
										"fid" : Ext.get("r_id").dom.value
									},
									success : function(response, action) {
										Ext.Msg.alert('', '保存成功！');
									},
									// 提交失败的回调函数
									failure : function() {
										Ext.Msg.alert('错误', '服务器出现错误请稍后再试！');
									}
								});
							}
						});
					}
				});
				
				var panelmenu = new Ext.Panel({
					region: "west",
					split:true,
		            minSize: 105,
		            maxSize: 400,
		            collapsible: true,
		            collapsed: false, //展开或关闭
		            margins:'0 0 0 0',
		            layout:'fit',
		            layoutConfig:{
		                animate:true
		            },
		            tbar : [btn_save_qx, '-', btn_clear_qx , {
						xtype : 'hidden',
						fieldLabel : '角色id2',
						id : 'r_id',
						name : 'fid'
					}, {
						xtype : 'hidden',
						fieldLabel : '角色name',
						id : 'r_name',
						name : 'fname'
					}],
					items :[tree_op],
					width:280
			   });
				
				
			    var panel = new Ext.Panel({
			    	region: "center",
					layout:'fit',
					border : true,
					margins:'0 5 0 0',
					tbar : [ btn_add, '-', btn_delete, '-', btn_sq, '-', btn_refresh ],
					items : [ grid]
				});
				
				
				
				
				
				/************************************* 所属区县树 结束 ********************************************************/
				var viewport = new Ext.Viewport({
					layout : 'border',
					items : [ panelmenu, panel ]
				});
			});
</script>


</html>